package com.sicilin.object.affiliated.company.action;

import com.sicilin.common.ajax.vo.AjaxResponse;
import com.sicilin.common.enums.ResultStateEnum;
import com.sicilin.common.exception.BusinessException;
import com.sicilin.object.affiliated.company.dto.Company;
import com.sicilin.object.affiliated.company.service.CompanyService;
import com.sicilin.system.login.service.LoginService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/companyAction")
public class CompanyAction {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/saveOrUpdateCompany.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("object:company:saveOrUpdate")
    public AjaxResponse saveCompany(@RequestBody Company company)throws Exception{
        if(company.getpCompanyId() == 0){throw new BusinessException("权限不足!");}
        return new AjaxResponse(ResultStateEnum.instances(companyService.saveCompany(company)));
    }

    @RequestMapping(value = "/saveOrUpdateCompany.json",method = RequestMethod.POST,params={"admin=true"})
    @ResponseBody
    @RequiresPermissions({"object:company:saveOrUpdate","admin"})
    public AjaxResponse saveCompanyAdmin(@RequestBody Company company)throws Exception{
        return new AjaxResponse(ResultStateEnum.instances(companyService.saveCompany(company)));
    }

    @RequestMapping(value = "/getCompanyById.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"object:company:search"})
    public AjaxResponse getCompanyById(@RequestParam("companyid") int companyid) throws Exception{
        return new AjaxResponse(companyService.getCompanyById(companyid,loginService.getCurrentUser().getCompanyId()));
    }
}
